//
//  LLStatusRetweetedView.swift
//  新浪微博
//
//  Created by mac on 16/5/28.
//  Copyright © 2016年 mac.com. All rights reserved.
//

import UIKit
import SnapKit

class LLStatusRetweetedView: UIView {
    var bottomCons: Constraint?
    var statusViewModel: LLStatusViewModel? {
        didSet {
            //绑定视图模型
            contentLabel.text = statusViewModel?.status?.retweeted_status?.text
            //设置配图视图的数据源
            pictureView.pictures = statusViewModel?.status?.retweeted_status?.pic_urls
            bottomCons?.uninstall()
            if  let  urls = statusViewModel?.status?.retweeted_status?.pic_urls where urls.count > 0{
            pictureView.pictures = urls
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
                pictureView.hidden = false
            }else{
                pictureView.pictures = nil
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
                pictureView.hidden = true

            }
        }
    }
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        self.backgroundColor = UIColor.whiteColor()
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置界面
    private func setupUI() {
        backgroundColor = UIColor(white: 0.90, alpha: 1)
        addSubview(contentLabel)
        addSubview(pictureView)
        contentLabel.snp_makeConstraints { (make) in
            make.top.left.equalTo(self).offset(StatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
        }
        
        //给转发微博的底部添加约束
        self.snp_makeConstraints { (make) in
           self.bottomCons =  make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    }
    //懒加载控件
    private lazy var contentLabel: UILabel = UILabel(title: "我也是王大锤,我再为自己带盐", font: 14, textColor: UIColor.darkGrayColor(), alignment: .Left)
    
    private lazy var pictureView: LLStatusPictureView = LLStatusPictureView()
}
    